import React, { useEffect } from "react";
import * as echarts from "echarts";
import axios from "axios";
import { useNavigate } from "react-router-dom";

const Index: React.FC = () => {
  const navigate = useNavigate()
  useEffect(() => {
    axios.get("/api/list").then((resp) => {
      const { data } = resp.data;

      const list = ["10-200", "201-400", "401-600", "601-800", "801-1000"];

      const arr = [0, 0, 0, 0, 0];
      data.forEach((v: any) => {
        const i = list.findIndex((item) => {
          const [min, max] = item.split("-");
          return min <= v.price && max >= v.price;
        });
        if (i > -1) {
          arr[i]++;
        }
      });

      const box = document.querySelector(".box");
      const myChart = echarts.init(box as HTMLElement);
      // 绘制图表
      myChart.setOption({
        title: {
          text: "销售量",
        },
        tooltip: {},
        xAxis: {
          data: list,
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "bar",
            data: arr,
          },
        ],
      });

      myChart.on('click', (params) => {
        const [min, max] = params.name.split('-');
        navigate(`/list?min=${min}&max=${max}`)
      })
    });

    const box1 = document.querySelector(".box1");
    const myChart1 = echarts.init(box1 as HTMLElement);
    const colors = ["#5470C6", "#EE6666"];

    const option = {
      color: colors,

      tooltip: {
        trigger: "none",
        axisPointer: {
          type: "cross",
        },
      },
      legend: {},
      grid: {
        top: 70,
        bottom: 50,
      },
      xAxis: [
        {
          type: "category",
          axisTick: {
            alignWithLabel: true,
          },
          axisLine: {
            onZero: false,
            lineStyle: {
              color: colors[1],
            },
          },
          axisPointer: {
            label: {
              formatter: function (params: any) {
                return (
                  "Precipitation  " +
                  params.value +
                  (params.seriesData.length
                    ? "：" + params.seriesData[0].data
                    : "")
                );
              },
            },
          },

          // prettier-ignore
          data: ['2016-1', '2016-2', '2016-3', '2016-4', '2016-5', '2016-6', '2016-7', '2016-8', '2016-9', '2016-10', '2016-11', '2016-12'],
        },
        {
          type: "category",
          axisTick: {
            alignWithLabel: true,
          },
          axisLine: {
            onZero: false,
            lineStyle: {
              color: colors[0],
            },
          },
          axisPointer: {
            label: {
              formatter: function (params: any) {
                return (
                  "Precipitation  " +
                  params.value +
                  (params.seriesData.length
                    ? "：" + params.seriesData[0].data
                    : "")
                );
              },
            },
          },

          // prettier-ignore
          data: ['2015-1', '2015-2', '2015-3', '2015-4', '2015-5', '2015-6', '2015-7', '2015-8', '2015-9', '2015-10', '2015-11', '2015-12'],
        },
      ],
      yAxis: [
        {
          type: "value",
        },
      ],
      series: [
        {
          name: "Precipitation(2015)",
          type: "line",
          xAxisIndex: 1,
          smooth: true,
          emphasis: {
            focus: "series",
          },
          data: [
            2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3,
          ],
        },
        {
          name: "Precipitation(2016)",
          type: "line",
          smooth: true,
          emphasis: {
            focus: "series",
          },
          data: [
            3.9, 5.9, 11.1, 18.7, 48.3, 69.2, 231.6, 46.6, 55.4, 18.4, 10.3,
            0.7,
          ],
        },
      ],
    };
    myChart1.setOption(option);
  }, []);

  return (
    <div>
      <div>首页</div>
      <div className="box" style={{ width: 600, height: 400 }}></div>
      <div className="box1" style={{ width: 400, height: 400 }}></div>
    </div>
  );
};

export default Index;
